//
//  AYSCollectViewController.m
//  AiYingShiProject
//
//  Created by lanou on 15/11/2.
//  Copyright (c) 2015年 李晓理. All rights reserved.
//

#import "AYSCollectViewController.h"
#import "AYSFavouriteHandle.h"
#import "AYSPlayDataModel.h"
#import "AYSCommonPlayViewController.h"
@interface AYSCollectViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) UITableView *tableView;
// 当前的编辑模式
@property (nonatomic,assign) UITableViewCellEditingStyle editingStyle;
@end

@implementation AYSCollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.titleLabel.text = @"我的影片";
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 65, SCREEN_WIDTH, SCREEN_HEIGHT - 65) style:(UITableViewStylePlain)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.allowsSelectionDuringEditing = YES;  // 编辑状态下可以选中
    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:_tableView];
    UIButton *deleteButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [deleteButton setBackgroundImage:[UIImage imageNamed:@"edit"] forState:(UIControlStateNormal)];
    [deleteButton setBackgroundImage:[UIImage imageNamed:@"over"] forState:(UIControlStateSelected)];
    deleteButton.frame = CGRectMake(SCREEN_WIDTH - 80, 30, 30, 30);
    [deleteButton addTarget:self action:@selector(cilckDeteleButton:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.topView addSubview:deleteButton];
    
    
    [[AYSFavouriteHandle shareInstance] setupMovieDataSource];
    
    // 注册cell
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"movie"];
}

- (void)cilckDeteleButton:(UIButton *)button
{
    button.selected = !button.selected;
    [self deleteMovie];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[AYSFavouriteHandle shareInstance]countOfMovie];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"movie" forIndexPath:indexPath];
    AYSPlayDataModel *movie = [[AYSFavouriteHandle shareInstance] movieForRow:indexPath.row];
    cell.textLabel.text = movie.programname;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    AYSPlayDataModel *movie = [[AYSFavouriteHandle shareInstance] movieForRow:indexPath.row];
    AYSCommonPlayViewController *playVC = [[AYSCommonPlayViewController alloc] init];
    playVC.programeID = movie.programID;
    [self.navigationController pushViewController:playVC animated:YES];
}

// 让tableview变成可编辑状态
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [_tableView setEditing:editing animated:animated];
}

// 指定哪一行的编辑按钮状态指定编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return _editingStyle;
}

// 判断点击按钮的样式来作删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        [[AYSFavouriteHandle shareInstance] deleteMovieForRow:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}

- (void)deleteMovie
{
    _editingStyle = UITableViewCellEditingStyleDelete;
    BOOL isEditing = self.tableView.isEditing;
    [self.tableView setEditing:!isEditing animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
